package com.zjuh.goodboy.misc;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.zjuh.goodboy.awires.ConfigurationAwire;
import com.zjuh.goodboy.config.Configuration;
import com.zjuh.goodboy.config.JdbcConfig;
import com.zjuh.goodboy.util.ClassUtil;

public class SimpleDataSource implements DataSource, ConfigurationAwire {

	private JdbcConfig jdbcConfig;
	
	public SimpleDataSource(JdbcConfig jdbcConfig) {
		super();
		this.jdbcConfig = jdbcConfig;
	}
	
	public SimpleDataSource() {
		jdbcConfig = new JdbcConfig();
	}

	protected String getDsPrefix() {
		return "";
	}
	
	@Override
	public void setConfiguration(Configuration configuration) {
		jdbcConfig.setDriverClassName(configuration.getProperty(getDsPrefix() + "jdbc.driver"));
		jdbcConfig.setUrl(configuration.getProperty(getDsPrefix() +"jdbc.url"));
		jdbcConfig.setUsername(configuration.getProperty(getDsPrefix() +"jdbc.username"));
		jdbcConfig.setPassword(configuration.getProperty(getDsPrefix() +"jdbc.password"));
	}

	@Override
	public Connection getConnection() throws SQLException {
		ClassUtil.newInstance(jdbcConfig.getDriverClassName());
		return DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUsername(), jdbcConfig.getPassword());
	}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		return null;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return null;
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return 0;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {

	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {

	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return null;
	}

}
